﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Engine.Dtd.Schema {
    [DebuggerDisplay("{Count}")]
    internal sealed class InclusionCollection : ICollection<DName> {
        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        private List<DName> items;
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private DContentModel model;

        public InclusionCollection(DContentModel model) {
            Guard.ArgumentNotNull(model, "model");
            this.model = model;
            this.items = new List<DName>();
            }

        public InclusionCollection(DContentModel model, IEnumerable<DName> collection) {
            Guard.ArgumentNotNull(model, "model");
            Guard.ArgumentNotNull(collection, "collection");
            this.model = model;
            this.items = new List<DName>();
            foreach (DName name in collection) {
                items.Add(name);
                }
            }

        public void Add(DName item) {
            Guard.ArgumentNotNull(item, "item");
            if (Contains(item))
                throw new ArgumentException("The item already exists in this coolection.");
            items.Add(item);
            model.InitializeInclusionExceptions();
            }

        public void Clear() {
            items.Clear();
            }

        public bool Contains(DName item) {
            for (int i = 0; i < Count; i++) {
                if (items[i] == item)
                    return true;
                }
            return false;
            }

        public void CopyTo(DName[] array, int arrayIndex) {
            if (arrayIndex < 0 || Count <= arrayIndex)
                throw new ArgumentOutOfRangeException("arrayIndex");
            for (int i = arrayIndex; i < Count; i++)
                array[i] = items[i];
            }

        public int Count {
            get { return items.Count; }
            }

        public bool IsReadOnly {
            get { return false; }
            }

        public bool Remove(DName item) {
            for (int i = 0; i < Count; i++) {
                if (items[i] == item) {
                    items.RemoveAt(i);
                    return true;
                    }
                }
            return true;
            }

        public IEnumerator<DName> GetEnumerator() {
            return items.GetEnumerator();
            }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return items.GetEnumerator();
            }
        }
    }
